<?php
namespace app\admin\controller;

use app\admin\controller\Base;
use think\Db;
use think\Request;
use app\admin\Model\Nx_wechat_firstmenu;

class Wechatmenu extends Base
{
    public function _initialize()
    {
        parent::_initialize();
    }
    public function menuList()
    {
        return $this->fetch('wechatmenu/menuList');
    }
    public function getMenuList()
    {
        $key = $this->request->post('keyword');
        $data = Db::table('nx_wechat_firstmenu')->where('delstatus','1')
            ->where('category|createtime','like','%'.$key.'%')
            ->select();
        foreach ($data as $k=>$v){
            $namearr = [];
            $ghidarr = explode(',',$v['ghid_list']);
            for($i=0;$i<count($ghidarr);$i++){
                $name = Db::table('nx_authorizer')->where('ghid',$ghidarr[$i])->find();
                $namearr[$i]=$name['name'];
            }
            $namelist = implode(',',$namearr);
            $data[$k]['ghid_listname'] = $namelist;
        }
        return $this->api_resp_success('','',$data);
    }
    public function addMenu()
    {
        return $this->fetch('wechatmenu/addMenu');
    }
    //定义回复菜单
    public function addBackContent()
    {
        return $this->fetch('wechatmenu/addBackContent');
    }
    public function getMessageList()
    {
        $keyword = $this->request->post('keyword');
        $list = Db::table('nx_imgtext_info')
            ->where('title','like','%'.$keyword.'%')
            ->where('delstatus','1')
            ->order('createtime','desc')
            ->select();
        return  $this->api_resp_success("",'',$list);
    }
    public function doAddMenu()
    {
        $category = $this->request->post('category');
        $dataarr = $this->request->post('dataarr');
        $leftiden = $this->request->post('leftiden');
        $leftname = $this->request->post('leftname');
        $middle = $this->request->post('middle');
        $middlename = $this->request->post('middlename');
        $right = $this->request->post('right');
        $rightname = $this->request->post('rightname');
        if($dataarr == ''){
            if($leftname !=''){
                $contnet['button'][0]['url'] = $leftiden;
                $contnet['button'][0]['type'] = "view";
                $contnet['button'][0]['name'] = $leftname;
            }
            if($middlename !=''){
                $contnet['button'][1]['url'] = $middle;
                $contnet['button'][1]['type'] = "view";
                $contnet['button'][1]['name'] = $middlename;
            }
            if($rightname !=''){
                $contnet['button'][2]['url'] = $right;
                $contnet['button'][2]['type'] = "view";
                $contnet['button'][2]['name'] = $rightname;
            }

        }else{
            if($leftname !=''){
                $contnet['button'][0]['name'] = $leftname;
                $contnet['button'][0]['sub_button'] = [];
            }
            if($middlename !=''){
                $contnet['button'][1]['name'] = $middlename;
                $contnet['button'][1]['sub_button'] = [];
            }
            if($rightname !=''){
                $contnet['button'][2]['name'] = $rightname;
                $contnet['button'][2]['sub_button'] = [];
            }
            foreach ($dataarr as $k=>$v){
                $datalist[$k] = $v;
                if($leftname !=''){
                    $contnet['button'][0]['sub_button'][$k]['type'] ="view";
                    $contnet['button'][0]['sub_button'][$k]['name'] =$v['leftname'];
                    $contnet['button'][0]['sub_button'][$k]['url'] =$v['lefturl'];
                }
                if($middlename !=''){
                    $contnet['button'][1]['sub_button'][$k]['type'] ="view";
                    $contnet['button'][1]['sub_button'][$k]['name'] =$v['middlename'];
                    $contnet['button'][1]['sub_button'][$k]['url'] =$v['middleurl'];
                }
                if($rightname !=''){
                    $contnet['button'][2]['sub_button'][$k]['type'] ="view";
                    $contnet['button'][2]['sub_button'][$k]['name'] =$v['rightname'];
                    $contnet['button'][2]['sub_button'][$k]['url'] =$v['righturl'];
                }
            }
        }
        $data = [
            'type'=>'view',
            'category'=>$category,
            'menuleftname'=>$leftname,
            'left_url'=>$leftiden,
            'menumiddlename'=>$middlename,
            'middle_url'=>$middle,
            'menurightname'=>$rightname,
            'right_url'=>$right,
            'content'=>json_encode($contnet,JSON_UNESCAPED_UNICODE),
            'createtime'=>date('Y-m-d H:i:s',time()),
        ];
        $result = Db::name('nx_wechat_firstmenu')->insertGetId($data);
        if($result){
            if($dataarr != ''){
                foreach ($datalist as $key=>$val){
                    $data2 = [
                        'firstmenuid'=>$result,
                        'menuleftname'=>$val['leftname'],
                        'left_url'=>$val['lefturl'],
                        'menumiddlename'=>$val['middlename'],
                        'middle_url'=>$val['middleurl'],
                        'menurightname'=>$val['rightname'],
                        'right_url'=>$val['righturl'],
                        'createtime'=>date('Y-m-d H:i:s',time()),
                    ];
                    Db::name('nx_wechat_secondmenu')->insert($data2);
                }
            }
            return $this->api_resp_success('','',$result);
        }
    }
    public function doDeleteMenu()
    {
        $ids = $this->request->post('chk_value');
        $res = Db::name('nx_wechat_firstmenu')->where('id','in',$ids)->update(['delstatus'=>4]);
        if($res){
            return $this->api_resp_success('','',$res);
        }
    }
    public function editMenu()
    {
        $id = $this->request->param('mid');
        $data = Db::table('nx_wechat_firstmenu')->where('id',$id)->find();
        $this->assign('data',$data);
        return $this->fetch('wechatmenu/editMenu');
    }
    public function getSecondMenuList()
    {
        $tid = $this->request->post('tid');
        $res = Db::name('nx_wechat_secondmenu')->where('firstmenuid',$tid)->select();
        if($res){
            return $this->api_resp_success('','',$res);
        }
    }
    public function setTencentGhid()
    {
        $catregory = Db::table('nx_category')
            ->where([
                'delstatus'=>1,
                'level'=>1
            ])
            ->select();
        $mid = $this->request->param('mid');
        $data = Db::table('nx_wechat_firstmenu')->where('id',$mid)->field('ghid_list')->find();
        $this->assign('category',$catregory);
        $this->assign('mid',$mid);
        $this->assign('data',$data);
        return $this->fetch('wechatmenu/setTencentGhid');
    }
    public function doSaveGhids()
    {
        $ghids = $this->request->post('ghids');
        $id  =$this->request->post('ids');
        $res = Db::name('nx_wechat_firstmenu')->where('id',$id)->update(['ghid_list'=>$ghids]);
        if($res){
            return $this->api_resp_success('','',$res);
        }
    }
    public function doSaveGhidsList()
    {
        $ghids = $this->request->post('ghids');
        $id  =$this->request->post('ids');
        $ghidslist = Db::name('nx_wechat_firstmenu')->where('id',$id)->field('ghid_list')->find();
        if($ghidslist != ''){
            $list = $ghidslist['ghid_list'].','.$ghids;
            $list = ltrim($list,',');
        }else{
            $list = $ghids;
        }
        $res = Db::name('nx_wechat_firstmenu')->where('id',$id)->update(['ghid_list'=>$list]);
        if($res){
            return $this->api_resp_success('','',$res);
        }
    }
    //获取公众号列表
    public function getTencentList()
    {
        $keyword = $this->request->post('keyword','');
        $category = $this->request->post('category','');
        $category2= $this->request->post('category2','');
        $num = 10;
        if($category !== ''){
            $map[] = ['category', '=', $category];
        }
        if($category2 !== ''){
            $map[] = ['category2','=', $category2];
            $num = 0;
        }
        $map[] = ['delstatus', '=', 1];
        $list = Db::table('nx_authorizer')
            ->where('name|ghid|company','like','%'.$keyword.'%')
            ->where($map)
            ->order('category','desc')
            ->limit($num) //限制下分页条数  其他数据查找就OK了 可以去掉
            ->select();
        foreach($list as $k=>$v){
            if($v['category']!=''){
                $categorylist = Db::table('nx_category')->where('id',$v['category'])->find();
                $list[$k]['category'] = $categorylist['categoryname'];
                if($categorylist){
                    $catregory2 = Db::table('nx_category')->where('id',$v['category2'])->find();
                    $list[$k]['category2'] = $catregory2['categoryname'];
                }
            }else{
                $list[$k]['category'] = '';
                $list[$k]['category2'] = '';
            }
        }
        return  $this->api_resp_success("",'',$list);
    }

    //清空公众号
    public function doClearMenu()
    {
        $ids = $this->request->post('chk_value');
        $res = Db::name('nx_wechat_firstmenu')->where('id','in',$ids)->update(['ghid_list'=>'','updatetime'=>date('Y-m-d H:i:s',time())]);
        if($res){
            return $this->api_resp_success('','',$res);
        }
    }
}
